package com.mo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mo.constant.Constants;
import com.mo.mapper.TClueRemarkmapper;
import com.mo.model.TClueRemark;
import com.mo.model.TUser;
import com.mo.query.ClueRemarkQuery;
import com.mo.service.ClueRemarkService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author mo
 * version
 */
@Service
public class ClueRemarkServiceImpl implements ClueRemarkService {
    @Resource
    private TClueRemarkmapper tClueRemarkmapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();
        BeanUtils.copyProperties(clueRemarkQuery,tClueRemark);
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tClueRemark.setCreateTime(new Date());
        tClueRemark.setCreateBy(user.getId());
        return tClueRemarkmapper.insertSelective(tClueRemark);
    }

    @Override
    public PageInfo<TClueRemark> getClueRemarkByPage(Integer current, ClueRemarkQuery clueRemarkQuery) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TClueRemark> list = tClueRemarkmapper.selectClueRemarkByPage(clueRemarkQuery);
        PageInfo<TClueRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delAClueRemarkById(Integer id) {
        //逻辑删除：不删除数据，只是修改一下数据的状态
        //物理删除: 真正的把数据从表里删除
        TClueRemark tClueRemark = new TClueRemark();
        tClueRemark.setId(id);
        tClueRemark.setDeleted(1);
        return tClueRemarkmapper.updateByPrimaryKeySelective(tClueRemark);
    }

    @Override
    public TClueRemark getClueRemarkById(Integer id) {
        return tClueRemarkmapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();
        BeanUtils.copyProperties(clueRemarkQuery, tClueRemark);
        tClueRemark.setEditTime(new Date());
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tClueRemark.setEditBy(user.getId());
        return tClueRemarkmapper.updateByPrimaryKeySelective(tClueRemark);
    }
}
